import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import sun.net.www.http.HttpClient;

import java.io.*;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        String cookies=null;
        String ts=null;
        String sign=null;
        String productId=null;
        List<Long> ids=new ArrayList<Long>();
        System.out.println("\t\t\t基金涨幅监测");
        Gson gson=new Gson();
        File configFile=new File("c:\\chartconfig.txt");
        try {
            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream(configFile),"utf-8"));
            String configStr = bufferedReader.readLine();
            cookies=bufferedReader.readLine();
            bufferedReader.close();
            String[] configSplit = configStr.split("\\|");

            for(int i=1;i<=configSplit.length;i++)
            {

                System.out.println(i+" :"+configSplit[i-1]);

            }
            System.out.print("要选择监听的：");
            Scanner scanner=new Scanner(System.in);
            int select = scanner.nextInt();
            productId=configSplit[select-1].split(":")[0];
            ts=configSplit[select-1].split(":")[1];
            sign=configSplit[select-1].split(":")[2];

            String param= "[{\"period\":\"lastYear\",\"productId\":\""+productId+"\"}]";
            String name=configSplit[select-1].split(":")[3];
            System.out.println("基金名称\t\t\t时间\t\t\t净值\t\t\t估算涨幅");
            while(true)
            {
                CloseableHttpClient httpClient= HttpClients.createDefault();
                HttpPost httpPost=new HttpPost("http://mobilegw.alipay.com/mgw.htm");
                httpPost.addHeader("nbappid","20000793");
                httpPost.addHeader("AppId","IOS_ALISTOCK");
                httpPost.addHeader("clientVersion","6.12.1.200");
                httpPost.addHeader("srcSpm","a153.b2503.c5640.d23359");
                httpPost.addHeader("Did","X/XAkGoIapUDABhcXLSTJ/Ip");
                httpPost.addHeader("nbversion","3.36.2012251249.21");
                httpPost.addHeader("pagets","a153.b2503__NRO14Pz_");
                httpPost.addHeader("Ts",ts);
                httpPost.addHeader("Cookie",cookies);
                httpPost.addHeader("package_nick","3.36.188");
                httpPost.addHeader("retryable2","0");
                httpPost.addHeader("bifrost_dto","19000");
                httpPost.addHeader("lastClickSpm","a153.b2503.c5640.d23359");
                httpPost.addHeader("Accept","*/*");
                httpPost.addHeader("Content-Type","application/json");
                httpPost.addHeader("Accept-Encoding","gzip, deflate, br");
                httpPost.addHeader("scene","active");
                httpPost.addHeader("Sign",sign);
                httpPost.addHeader("User-Agent","%E8%9A%82%E8%9A%81%E8%B4%A2%E5%AF%8C/6.12.1.200 CFNetwork/1128.0.1 Darwin/19.6.0");
                httpPost.addHeader("Operation-Type","com.alipay.wealthbffweb.fund.vega.estimate.query");
                httpPost.addHeader("x-nb-appid","AF_20000793_ios");
                httpPost.addHeader("Connection","keep-alive");
                StringEntity stringEntity=new StringEntity(param,"utf-8");
                httpPost.setEntity(stringEntity);
                CloseableHttpResponse execute = httpClient.execute(httpPost);
                if(execute.getStatusLine().getStatusCode()==200)
                {
                    String contentEntity = EntityUtils.toString(execute.getEntity());
                    JsonObject data = gson.fromJson(contentEntity, JsonObject.class);
                    JsonArray datas = data.get("result").getAsJsonObject().get("data").getAsJsonArray();
                    for(int i=0;i<datas.size();i++)
                    {
                        JsonObject item = datas.get(i).getAsJsonObject();
                        long time = item.get("time").getAsLong();
                        if(ids.contains(time))
                        {
                            continue;
                        }else{
                            ids.add(time);
                            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
                            String timeStr= simpleDateFormat.format(new Date(time));
                            BigDecimal bg = new BigDecimal((item.get("growth").getAsDouble())*100).setScale(2, BigDecimal.ROUND_HALF_UP);

                            System.out.println(name+"\t"+timeStr+"\t"+item.get("netValue").getAsDouble()+"\t"+bg.doubleValue()+"%");

                        }
                    }
                    // System.out.println(contentEntity);
                }else{
                    System.out.println("数据返回错误");
                }
                httpClient.close();
                Thread.sleep(60000);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
